package com.das.mechanic_base.adapter.groundpush;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.utils.SpHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 赠送余额adapter
 */
public class X3SelectGiftBalanceAdapter extends RecyclerView.Adapter<X3SelectGiftBalanceAdapter.SelectBrandHolder> {

    private Context mContext;

    private List<String> mList;

    private List<Boolean> bList;
    private String moneyUnit;

    public X3SelectGiftBalanceAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.bList = new ArrayList<>();
        moneyUnit = (String) SpHelper.getData(X3Constent.MONEYCODE, "£");
    }

    public void changeSelect(List<String> mList) {
        this.mList = mList;
        this.bList.clear();
        moneyUnit = (String) SpHelper.getData(X3Constent.MONEYCODE, "£");
        for (int i = 0; i < mList.size(); i++) {
            bList.add(i, false);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SelectBrandHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SelectBrandHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_gift_balance_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull SelectBrandHolder holder, int position) {
        String selectMoney=mList.get(position);

        holder.select_money_tv.setText(String.format(mContext.getString(R.string.x3_gift_money_custom_right_tv),selectMoney,moneyUnit));

        holder.select_money_tv.setTextColor(bList.get(position) ? Color.parseColor("#0077ff") : Color.parseColor("#666666"));
        holder.select_money_tv.setBackground(bList.get(position) ? mContext.getDrawable(R.drawable.x3_shape_idea_select_item) : mContext.getDrawable(R.drawable.x3_shape_idea_unselect_item));

        holder.itemView.setOnClickListener(v -> {
            for (int i = 0; i < bList.size(); i++) {
                bList.remove(i);
                bList.add(i, i == position);
            }
            notifyDataSetChanged();
            if (iOnClickSelect != null) {
                iOnClickSelect.iOnClickSelectBrand(selectMoney,bList.get(position));
            }
        });

    }

    IOnClickSelect iOnClickSelect;

    public void setiOnClickSelect(IOnClickSelect iOnClickSelect) {
        this.iOnClickSelect = iOnClickSelect;
    }

    public interface IOnClickSelect {
        void iOnClickSelectBrand(String selectMoney,boolean select);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class SelectBrandHolder extends RecyclerView.ViewHolder {
        TextView select_money_tv;

        public SelectBrandHolder(@NonNull View itemView) {
            super(itemView);
            select_money_tv = itemView.findViewById(R.id.select_money_tv);
        }
    }
}
